package cn.lxh.mq.consumer;

import cn.lxh.constants.Constants;
import cn.lxh.pojo.app.bo.PayOrderBo;
import cn.lxh.service.IPayOrderService;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

/**
 * @author lxh Create Time: 2024/1/15 23:43
 * 消费者
 */
@RocketMQMessageListener(
        consumerGroup = Constants.MQ.PAY_ORDER_CONSUMER, // 消费者名
        topic = Constants.MQ.PAY_ORDER_TOPIC, // 主题
        selectorExpression = Constants.MQ.PAY_ORDER_TAGS // 标签
)
@Component
@Slf4j
public class PayOrderConsumer implements RocketMQListener<MessageExt> {

    @Autowired
    private IPayOrderService payOrderService;

    @Override
    public void onMessage(MessageExt message) {
        // message.getBody()是byte数组
        String bodyStr = new String(message.getBody(), StandardCharsets.UTF_8);
        // 将字符串转为JSON对象
        PayOrderBo payOrderBo = JSON.parseObject(bodyStr, PayOrderBo.class);
        // 创建支付单
        payOrderService.createPayOrder(payOrderBo);
    }
}
